#LinuxCentos安装ffmpeg
set -e

#获取发行版本号以及yum或者apt
Get_Dist_Name()
{
    if grep -Eqii "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
        DISTRO='CentOS'
        PM='yum'
        CPLUG='gcc-c++'
    elif grep -Eqi "Red Hat Enterprise Linux Server" /etc/issue || grep -Eq "Red Hat Enterprise Linux Server" /etc/*-release; then
        DISTRO='RHEL'
        PM='yum'
        CPLUG='gcc-c++'
    elif grep -Eqi "Aliyun" /etc/issue || grep -Eq "Aliyun" /etc/*-release; then
        DISTRO='Aliyun'
        PM='yum'
        CPLUG='gcc-c++'
    elif grep -Eqi "Fedora" /etc/issue || grep -Eq "Fedora" /etc/*-release; then
        DISTRO='Fedora'
        PM='yum'
        CPLUG='gcc-c++'
    elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
        DISTRO='Debian'
        PM='apt'
        CPLUG='g++'
    elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
        DISTRO='Ubuntu'
        PM='apt'
        CPLUG='g++'
    elif grep -Eqi "Raspbian" /etc/issue || grep -Eq "Raspbian" /etc/*-release; then
        DISTRO='Raspbian'
        PM='apt'
        CPLUG='g++'
    else
        DISTRO='unknow'
        exit
    fi
}
Get_Dist_Name

#安装gcc和g++
$PM install -y gcc
$PM install -y $CPLUG

url='http://disk.geekdm.com/tool/'

dir='/usr/local/src/'

files=('nasm-2.15.05.tar.gz' 'yasm-1.3.0.tar.gz' 'pkg-config-0.29.tar.gz' 'x264-master.tar.gz' 'FFmpeg-n4.3.2.tar.gz')

looplen=$[${#files[*]}-1]

for i in $(seq 0 $looplen);
	
	#下载源码包
	do wget -O $dir${files[$i]} $url${files[$i]}
	
	#安装包绝对路径	
	filename=$dir${files[$i]}

	#安装包名字		
	name=${files[$i]}

	#安装nasm-2.15.05.tar.gz需要使用 tar -xvf解压
	if [ "$name" = "nasm-2.15.05.tar.gz" ]; then
		tar -xvf $filename -C $dir
	else
		tar -zxvf $filename -C $dir
	fi

	
	cd ${filename%.tar.gz}
	
	case $name in
		x264-master.tar.gz)	
			./configure --enable-shared
			;;
		FFmpeg-n4.3.2.tar.gz)
			./configure  --enable-gpl --enable-nonfree --enable-shared --prefix=/usr/local/ffmpeg  --enable-version3 --enable-libx264
			;;
		pkg-config-0.29.tar.gz)
			./configure --with-internal-glib
			;;
		*)
			./configure
			;;
	esac	
	make && make install
done

#ld.so.conf配置
echo -e "/usr/local/ffmpeg/lib\n/usr/local/lib" >> /etc/ld.so.conf
ldconfig

#试运行
/usr/local/ffmpeg/bin/ffmpeg -version


#成功
echo '安装成功，success'
